var ActicleTypeId;
layui.config({
	version:'1510164124181'
}).use(['form','laydate', 'laypage', 'layer', 'element','upload','jquery'],function(){
	form = layui.form;
	layer = layui.layer;
	upload = layui.upload;
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作

	ActicleTypeId=GetQueryString("ActicleTypeId");
	if(!isNotNull(ActicleTypeId)){
		ActicleTypeId=0;
		$("#title_name").html("添加商品分类");
	}else{
		$("#title_name").html("修改商品分类");
		SelectActicleTypeListById();
	}
	//layui普通图片上传
	let uploadInst = upload.render({
		elem: '#uploadimgs'
		,url: httpurl+'/uploadImgQiniu?pathName=classifyList&&divisor=0&dividend=1'
		,before: function(obj){
			//预读本地文件示例，不支持ie8
			obj.preview(function(index, file, result){
				$("#divpreview").css("display","block");
				$('#preview').attr('src', result); //图片链接（base64）
			});
		}
		,done: function(res){
			console.log(JSON.stringify(res)+"=======done");
			if(res.rtnCode==200){//如果上传失败
				$('#preview').attr('name', res.msg); //图片链接（base64）
			}else{
				$("#divpreview").css("display","none");
				$('#preview').attr('src', ""); //图片链接（base64）
				return layer.msg(res.msg);
			}

		}
		,error: function(){
			//演示失败状态，并实现重传
			var demoText = $('#demoText');
			demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn  demo-reload">重试</a>');
			demoText.find('.demo-reload').on('click', function(){
				uploadInst.upload();
			});
		}
	});
});
function SelectActicleTypeListById(){
	$.ajax({
		url: httpurl+"/findIdHT",
		cache: false,
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		data: {
			"id":ActicleTypeId,
		},
		success: function(data) {
			if(data.rtnCode==200){
				var dataInfo=data.data;
				$("#titles").val(dataInfo.name);
				$("#sort").val(dataInfo.sort);
				$("#type option[value="+dataInfo.type+"]").attr("selected","selected");
				$("#IsEnable input[value="+dataInfo.enable+"]").attr("checked",true);
				if(dataInfo.pimg!=null&&dataInfo.pimg!=""&&dataInfo.pimg!=undefined){
					$("#preview").attr("src",visitimgurl+"/"+dataInfo.pimg);
					$("#preview").attr("name",dataInfo.pimg);
					$("#divpreview").show();
				}
			}
		}
	})
}
function insertHlinesType(){
	let type =2;
	let title_info=$("#titles").val();//文章标题
	if(!isNotNull(title_info)){
		top.layer.msg("请输入文章标题!");
		return;
	}
	let sort=$("#sort").val();
	if(!isNotNull(sort)){
		sort=0;
	}
	//商品主图
	let divpreview_img=$("#preview").attr("name");
	if(!isNotNull(divpreview_img)){
		top.layer.alert("请添加图片！");
		return;
	}
	let enable=$("#IsEnable input[name='enable']:checked").val();
	top.layer.confirm('是否确定提交？',{icon: 3,title:'提示信息'},function(index){
		// var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
		let tempHttp="updateTypeList";
		if(ActicleTypeId==0){
			tempHttp="insertTypeList";
		}
		setTimeout(function(){
			$.ajax({
				type:"POST",
				url: httpurl+"/"+tempHttp,
				cache: false,
				headers: {
					"Access-Control-Allow-Origin": "*",
					"Access-Control-Allow-Methods": "*",
					"Access-Control-Allow-Headers": "Authorization",
					"Authorization": GetTokenInfo()
				},
				contentType : 'application/json',
				data: JSON.stringify({
					"id":ActicleTypeId,
					"name":title_info,
					"enable":enable,
					"sort":sort,
					"type":type,
					"pimg":divpreview_img
				}),
				success: function(data) {
					if(data.rtnCode==200){
						top.layer.closeAll();
						top.layer.msg(data.msg);
						return_list();
					}
				}
			});
		},1000);
	})
}
function return_list(){
	window.location.href="ClassifyList.html";
}
